/**
 * @author Yin Wang
 */

package edu.cmu.cs.webapp.ioms.views;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

import edu.cmu.cs.webapp.ioms.models.util.FinancialRecord;
import edu.cmu.cs.webapp.ioms.models.util.FinancialReport;
import edu.cmu.cs.webapp.ioms.ui.providers.finreport.FinReportTableViewerContentProvider;
import edu.cmu.cs.webapp.ioms.ui.providers.finreport.FinReportTableViewerLabelProvider;
import edu.cmu.cs.webapp.ioms.utils.IOMSUtils;
import edu.cmu.cs.webapp.ioms.utils.SWTUtils;

public class FinancialReportView extends ViewPart {

	public static final String ID_VIEW = "edu.cmu.cs.webapp.ioms.views.financialreport";

	private TableViewer viewer;
	private Combo periodCom;
	private Table table;
	private Button genBtn;
	private DateTime fromDate;
	private DateTime toDate;
	ITableLabelProvider labelProvider;
	private FinancialReport finReport;

	public FinancialReportView() {
		labelProvider = new FinReportTableViewerLabelProvider();
	}

	@Override
	public void createPartControl(Composite parent) {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		gridLayout.marginHeight = 2;
		gridLayout.marginWidth = 2;
		parent.setLayout(gridLayout);

		Composite composite = new Composite(parent, SWT.NONE);
		GridData gridCompositeData = new GridData();
		gridCompositeData.verticalAlignment = GridData.BEGINNING;
		gridCompositeData.horizontalAlignment = GridData.FILL;
		gridCompositeData.grabExcessHorizontalSpace = true;

		composite.setLayoutData(gridCompositeData);

		gridLayout = new GridLayout();
		gridLayout.numColumns = 7;
		composite.setLayout(gridLayout);

		SWTUtils.createLabel(composite, "Period: ", 1);

		periodCom = SWTUtils.createCombo(composite, new String[] { "Month",
				"Quarter", "Year" }, 1);
		periodCom.select(0);

		SWTUtils.createLabel(composite, "From: ", 1);
		fromDate = new DateTime(composite, SWT.DROP_DOWN);
		SWTUtils.createLabel(composite, "To: ", 1);
		toDate = new DateTime(composite, SWT.DROP_DOWN);

		// SWTUtils.createLabel(composite, "Client Name: ", 1);

		// clientTxt = new Text(composite, SWT.BORDER);
		// GridData txtGridData = new GridData(GridData.FILL_HORIZONTAL);
		// txtGridData.horizontalAlignment = SWT.FILL;
		// txtGridData.grabExcessHorizontalSpace = true;
		// clientTxt.setLayoutData(txtGridData);
		genBtn = SWTUtils.createPushButton(composite, "Generate");
		genBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent event) {
				// implementation
				setReport();
			}
		});

		createTableViewer(parent);

	}

	public void createTableViewer(Composite parent) {

		/*
		 * Create table
		 */
		GridData gridData = new GridData();
		gridData.horizontalSpan = 4;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;

		table = new Table(parent, SWT.FULL_SELECTION | SWT.BORDER
				| SWT.H_SCROLL | SWT.V_SCROLL);
		table.setLayoutData(gridData);

		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		// period
		TableColumn periodColumn = new TableColumn(table, SWT.NONE);
		periodColumn.setText("Period"); //$NON-NLS-1$
		periodColumn.setWidth(150);
		periodColumn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int sortDirection = IOMSUtils
						.updateSortDirection((TableColumn) e.widget);
				viewer.setSorter(new FinancialReportSorter(
						FinReportTableViewerLabelProvider.FIN_PERIOD,
						sortDirection == SWT.DOWN));
			}
		});

		// Revenue
		TableColumn revenueColumn = new TableColumn(table, SWT.NONE);
		revenueColumn.setText("Revenue"); //$NON-NLS-1$
		revenueColumn.setWidth(150);
		revenueColumn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int sortDirection = IOMSUtils
						.updateSortDirection((TableColumn) e.widget);
				viewer.setSorter(new FinancialReportSorter(
						FinReportTableViewerLabelProvider.FIN_REVENUE,
						sortDirection == SWT.DOWN));
			}
		});

		// Cost
		TableColumn costColumn = new TableColumn(table, SWT.NONE);
		costColumn.setText("Total Cost"); //$NON-NLS-1$
		costColumn.setWidth(150);
		costColumn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int sortDirection = IOMSUtils
						.updateSortDirection((TableColumn) e.widget);
				viewer.setSorter(new FinancialReportSorter(
						FinReportTableViewerLabelProvider.FIN_COST,
						sortDirection == SWT.DOWN));
			}
		});

		// profit
		TableColumn profitColumn = new TableColumn(table, SWT.NONE);
		profitColumn.setText("Profit"); //$NON-NLS-1$
		profitColumn.setWidth(150);
		profitColumn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int sortDirection = IOMSUtils
						.updateSortDirection((TableColumn) e.widget);
				viewer.setSorter(new FinancialReportSorter(
						FinReportTableViewerLabelProvider.FIN_PROFIT,
						sortDirection == SWT.DOWN));
			}
		});

		/*
		 * Create viewer
		 */
		viewer = new TableViewer(table);

		viewer.setUseHashlookup(true);
		viewer.setLabelProvider(labelProvider);
		viewer.setContentProvider(new FinReportTableViewerContentProvider());
		viewer.setInput(finReport);
	}

	public void setReport() {

		finReport = new FinancialReport();
		int periodIndex = periodCom.getSelectionIndex();
		int fromYear = fromDate.getYear();
		int fromMonth = fromDate.getMonth() + 1;
		int toYear = toDate.getYear();
		int toMonth = toDate.getMonth() + 1;
		if (periodIndex == 0) {
			finReport.setByMonth(fromYear, fromMonth, toYear, toMonth);
		}
		if (periodIndex == 1) {
			finReport.setByQuarter(fromYear, fromMonth, toYear, toMonth);
		}
		if (periodIndex == 2) {
			finReport.setByYear(fromYear, fromMonth, toYear, toMonth);
		}

		viewer.setInput(finReport);
	}

	@Override
	public void setFocus() {

	}

	private class FinancialReportSorter extends ViewerSorter {
		private int columnIndex;
		private final boolean ascending;

		public FinancialReportSorter(int criteria, boolean ascending) {
			super();
			this.columnIndex = criteria;
			this.ascending = ascending;
		}

		@SuppressWarnings("unchecked")
		@Override
		public int compare(Viewer viewer, Object e1, Object e2) {
			int result = 0;
			if (e1 instanceof FinancialRecord && e2 instanceof FinancialRecord) {
				result = getComparator().compare(
						labelProvider.getColumnText(e1, columnIndex),
						labelProvider.getColumnText(e2, columnIndex));
			}

			if (!ascending) {
				result = result * -1;
			}
			return result;
		}
	}

}
